/*
 * @Filename: UserController.java
 * @CreateTime: 2015-11-13 14:16:44
 * @Version: 1.0
 * @All rights (are) reserved.
 * @author miklechun
 */
package com.wonders.qxmanager.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wonders.qxmanager.model.Url;
import com.wonders.qxmanager.service.UrlService;
import com.wonders.qxmanager.utils.PageArrayList;
import com.wonders.qxmanager.utils.PageUIHelper;
import com.wonders.qxmanager.utils.PageUIHelper.Page;

/**
 * user web控制器
 * 
 * @author miklechun
 * 
 */
@Controller
public class UrlController {

	@Autowired
	private UrlService urlService;
	List<Url> list;
	@RequestMapping("/url/index.do")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		return new ModelAndView("/url/list.jsp");
	}

	@RequestMapping("/url/info.do")
	public ModelAndView info(HttpServletRequest req, HttpServletResponse res)
			throws IOException {

		return new ModelAndView("/url/info.jsp");
	}

	@RequestMapping("/url/list.do")
	public void list(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		req.setCharacterEncoding("UTF-8");
		 list =urlService.getAll();
		 JSONArray ja = new JSONArray();
		 
			for(Url url:list){
				ja.put(urlToJson(url));
			}
		
			JSONObject returnjs = new JSONObject();
			
			returnjs.put("total", list.size());
			returnjs.put("rows", ja);
			
			PrintWriter out = res.getWriter();
			out.write(returnjs.toString());
			res.setContentType("text/json; charset=UTF-8");		
	}

	@RequestMapping("/url/edit.do")
	public ModelAndView edit(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		req.setCharacterEncoding("UTF-8");
		Url url = new Url();
		url.setNo(Integer.parseInt(req.getParameter("no")));
		url.setName(req.getParameter("name"));
		url.setUrl(req.getParameter("url"));
		url.setPid(Integer.parseInt(req.getParameter("pid")));
		
		urlService.update(url);
		return null;
	}

	@RequestMapping("/url/save.do")
	public void save(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		Url url = new Url();
		url.setNo(Integer.parseInt(req.getParameter("no")));
		url.setName(req.getParameter("name"));
		url.setUrl(req.getParameter("url"));
		url.setPid(Integer.parseInt(req.getParameter("pid")));
		urlService.insert(url);	
		PageUIHelper.writeFormResponse(res, "{errorMsg:false}");
	}

	@RequestMapping("/url/remove.do")
	public void remove(HttpServletRequest req, HttpServletResponse res) {
		urlService.delete(Integer.parseInt(req.getParameter("id")));
	}
	
	private static JSONObject urlToJson(Url url) {
		
		JSONObject js = new JSONObject();
		js.put("id", url.getId());
		js.put("no", url.getNo());
		js.put("name", url.getName());
		js.put("url", url.getUrl());
		js.put("pid", url.getPid());
		
		return js;
	}

}
